package com.woniuxy.mapper.impl;

import com.woniuxy.mapper.StuDao;
import com.woniuxy.entity.Stu;

import java.sql.Connection;
import java.util.List;

/**
 * @author 作者
 * @create 2025-06-16 16:46
 */
public class StuDaoImpl implements StuDao {
    @Override
    public void insert(Stu stu) throws Exception{
        Connection connection = JdbcUtil.getConnection();
        String sql="insert into stu values(?,?,?)";
        JdbcUtil.executeUpdate(connection, sql, stu.getSid(),stu.getSname(),stu.getAge());
    }

    @Override
    public void deleteBySid(int sid) throws Exception {
        Connection connection = JdbcUtil.getConnection();
        String sql="delete from stu where sid=?";
        JdbcUtil.executeUpdate(connection, sql, sid);
    }

    @Override
    public void update(Stu stu) throws Exception {
        Connection connection = JdbcUtil.getConnection();
        String sql="update stu set sname=? , age=? where sid=?";
        JdbcUtil.executeUpdate(connection, sql, stu.getSname(), stu.getAge(),stu.getSid());
    }

    @Override
    public Stu selectBySid(int sid) throws Exception {
        Connection connection = JdbcUtil.getConnection();
        String sql="select * from stu where sid=?";
        return JdbcUtil.executeQueryOne(connection, Stu.class, sql, sid);
    }

    @Override
    public Stu selectBySname(String sname) throws Exception {
        Connection connection = JdbcUtil.getConnection();
        String sql="select * from stu where sname=?";
        return JdbcUtil.executeQueryOne(connection, Stu.class, sql, sname);
    }

    @Override
    public List<Stu> selectAll()throws Exception {
        Connection connection = JdbcUtil.getConnection();
        String sql="select * from stu ";
        return JdbcUtil.executeQuery(connection, Stu.class, sql);
    }

}
